<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\StudentWork;
use App\Services\StudentEvaluationService;

use Illuminate\Support\Facades\Validator;

use Illuminate\Http\Request;

class StudentEvaluationController extends AdminController
{
    public function updateStudentWork(Request $request){
        $validator = Validator::make($request->all(), [
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'score' => 'required|numeric',
            'evaluate_users_id' => 'required|integer|digits_between:1,20|min:1',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $putData = $request->except(['_method','_token']);
        $studentWorkk = new StudentWork();
        $query = $studentWorkk->where('course_section_id',$request->course_section_id)
            ->where('users_id',$request->users_id)->get()->toArray();
        if($query){
            $studentWorkk->where('course_section_id',$request->course_section_id)
                ->where('users_id',$request->users_id)
                ->update($putData);
        }else{
            StudentWork::create($putData);

        }

        return $this->success();
    }
    public function  listSlass(Request $request){
        $validator = Validator::make($request->all(), [
            'school_id' => 'required|integer|digits_between:1,20|min:1',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $school_id = $request->input('school_id');
        $course_id = $request->input('course_id');
        $list = StudentEvaluationService::getInstance()->listClass($school_id,$course_id);
        return $this->success($list);
    }
    public function listStudentWork(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'name' => 'string',
            'class_grades_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',

            'type' => 'integer|digits_between:1,20|min:1',
            'sort' => 'string',
            'order' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name= $request->input('name');
        $class_grades_id = $request->input('class_grades_id');
        $course_section_id = $request->input('course_section_id');
        $type = $request->input('type');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','student_works.created_at');
        $order = $request->input('order','desc');

        $list = StudentEvaluationService::getInstance()->listStudentWork($name,$class_grades_id,$course_section_id,$page,$limit,$sort,$order);

        return $this->success($list);

    }

//updateStudentWork
}
